package com.wing.product.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprisecab.feign.EnterpriseCabClient;
import com.wing.order.model.entity.CabInventoryProduct;
import com.wing.order.service.CabInventoryProductService;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.service.ProductDetailService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@Api(value = "Admin-产品明细表管理", tags = "#/product/productDetailSKU")
@RestController("productDetailSKUController")
@RequestMapping("product/admin/productDetailSKU")
@RequiredArgsConstructor
public class ProductDetailSKUController {
    private final ProductDetailService productDetailService;
    private final EnterpriseCabClient enterpriseCabClient;
    private final CabInventoryProductService cabInventoryProductService;

    @ApiOperation(value = "查询所有产品明细表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductDetail> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String skuCode,
            String skuName,
            Long productTypeId) {
        Page<ProductDetail> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", StrUtil.toUnderlineCase(order));
        paramMap.put("skuCode", skuCode);
        paramMap.put("skuName", skuName);
        paramMap.put("productTypeId", productTypeId);
        IPage<ProductDetail> list = productDetailService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加产品明细表", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(ProductDetail productDetail) {
        if (productDetailService.save(productDetail)) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改产品明细表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@RequestBody ProductDetail productDetailParam) {
        ProductDetail productDetail = productDetailService.getById(productDetailParam.getId());
        if (productDetail == null) {
            return JsonResult.fail("修改失败！");
        }
        if (productDetailService.updateById(productDetailParam)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "删除产品明细表", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productDetailId", value = "产品明细表id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long productDetailId) {
        if (productDetailService.removeById(productDetailId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "查询所有产品明细(不分页)", notes = "查询所有产品明细(不分页)")
    @GetMapping("/getProductDetailByProductId")
    public JsonResult<ProductDetail> getProductDetailByProductId(Long productId) {

        QueryWrapper<ProductDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        List<ProductDetail> productDetailList = productDetailService.list(wrapper);
        return JsonResult.success(productDetailList);
    }

    @ApiOperation(value = "查询产品详情", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuCode", value = "skuCode", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/getProductDetailBySkuCode")
    public JsonResult<ProductDetail> getProductDetailBySkuCode(String skuCode) {

        ProductDetail info = productDetailService.getProductDetailBySkuCode(skuCode);
        return JsonResult.success(info);
    }

    /**
     * 查询出库计划商品信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getProductDetailPageList")
    public PageResult<ProductDetail> getProductDetailPageList(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , ProductDetail productDetail) {
        //搜索
        IPage<ProductDetail> list = productDetailService.getProductDetailPageList(productDetail, page, limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有产品", notes = "查询")
    @GetMapping("/getAll")
    public JsonResult<ProductDetail> getAll() {
        QueryWrapper<ProductDetail> wrapper = new QueryWrapper<>();
        wrapper.select("sku_code", "sku_name", "product_type_id");
        List<ProductDetail> productDetail = productDetailService.list(wrapper);
        return JsonResult.success(productDetail);
    }

    /**
     * 查询在库产品列表
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getInventoryProductDetailPageList")
    public PageResult<ProductDetail> getInventoryProductDetailPageList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(value = "skuName", required = false) String skuName,
            @RequestParam(value = "skuCode", required = false) String skuCode,
            @RequestParam(value = "productTypeId", required = false) Long productTypeId,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "type", required = false) Integer type) {
        //搜索
        IPage<ProductDetail> list = productDetailService.getInventoryProductDetailPageList(page, limit, skuName, skuCode, productTypeId, id, type);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "修改产品明细表", notes = "修改")
    @PutMapping("/updateProductDetail")
    public JsonResult updateProductDetail(@RequestParam(value = "skuCode", required = false) String skuCode) {
        ProductDetail productDetailResult = productDetailService.getProductDetailBySkuCode(skuCode);
        if (productDetailResult == null) {
            return JsonResult.fail("修改失败！");
        }
        if (productDetailService.updateById(productDetailResult)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    /**
     * @param id  产品库存id
     * @param num 采购数
     * @return
     */
    @GetMapping("/upById")
    public JsonResult upById(@RequestParam("id") Long id, @RequestParam("num") Integer num) {
        CabInventoryProduct inventoryProduct = cabInventoryProductService.getById(id);
        inventoryProduct.setQuantity(inventoryProduct.getQuantity() - num);
        if (cabInventoryProductService.updateById(inventoryProduct)) {
            return JsonResult.success("修改成功");
        }
        return JsonResult.fail();
    }

}
